package pages;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.PageFactory;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import utils.PropertyLoader;


public abstract class BasePage {
    protected static  WebDriver driver;
    protected String pageUrl;
    protected String websiteUrl;

    /*---------------locators---------------------------------------------------------------*/
    private static By autorizeButton = By.className("m-user-link");
    private static By loginField = By.cssSelector(".auth-f input[name=login]");
    private static By passwordField = By.cssSelector(".auth-f input[name=password]");
    private static By enterCabinetButton = By.cssSelector(".auth-f button");
    private static By searchInput = By.cssSelector(".header-search-field-text input[type=text]");
    private static By searchSubmit = By.cssSelector(".header-search-submit-wrap button[type=submit]");
    private static By mainMenu = By.id("action-zone");

    /*---------------constructor---------------------------------------------------------------*/
    public BasePage(WebDriver driver, String pageUrl)
    {
        websiteUrl = PropertyLoader.loadProperty("site.url");
        this.pageUrl = websiteUrl + pageUrl;
        this.driver = driver;
        PageFactory.initElements(driver, this);
    }

    /*---------------methods---------------------------------------------------------------*/
    abstract public void open();

    public boolean isOpened() {
        Boolean result = driver.getCurrentUrl().equals(pageUrl);
        return result;
    }

    public String getPageUrl() {
        return pageUrl;
    }

    // get url without GET parameters
    public String getPath(String url) {

        String[] pathParts = url.split("\\?");
        String path = pathParts[0];
        return path;

    }

    // get hash like "#cart" from url, helps to define popups
    public String getHashOfPageUrl(String url) {
        int sharpPos = url.indexOf('#');
        String q = null;
        if (sharpPos >= 0) {
            q = url.substring(sharpPos);
        }
        return q;
    }

    /*---------------Autorization block---------------------------------------------------------------*/
    public void openAutorizationPopup() {
        driver.findElement(autorizeButton).click();
    }

    public void autorize(String login, String password) {
        driver.findElement(loginField).clear();
        driver.findElement(loginField).sendKeys(login);
        driver.findElement(passwordField).clear();
        driver.findElement(passwordField).sendKeys(password);
        driver.findElement(enterCabinetButton).click();

        (new WebDriverWait(driver, 20))
                .until(ExpectedConditions.presenceOfElementLocated(By.className("m-user-signout")));

    }

    public Boolean isAutorized() {

        return driver.findElements(By.className("m-user-signout")).size() != 0;

    }

    public void logout() {

        driver.findElement(By.name("signout")).click();

        (new WebDriverWait(driver, 20))
                .until(ExpectedConditions.presenceOfElementLocated(By.className("m-user-signin")));

    }



    /*---------------Search block---------------------------------------------------------------*/
    public void search(String searchText) {

        driver.findElement(searchInput).clear();
        driver.findElement(searchInput).sendKeys(searchText);
        driver.findElement(searchSubmit).click();

        // switch focus of WebDriver to the next found window handle (that's your newly opened window)
        for (String winHandle : driver.getWindowHandles()) {
            driver.switchTo().window(winHandle);
        }

        (new WebDriverWait(driver, 20))
                .until(ExpectedConditions.presenceOfElementLocated(By.className("search-result")));

    }

    /*---------------Main menu block---------------------------------------------------------------*/
    public String openMainMenuItem (String id) {
        WebElement menuLink  = driver.findElement(mainMenu).findElement(By.id(id)).findElement(By.cssSelector(".m-main-i a"));
        String menuLinkHref = menuLink.getAttribute("href");
        menuLink.click();

        // switch focus of WebDriver to the next found window handle (that's your newly opened window)
        for (String winHandle : driver.getWindowHandles()) {
            driver.switchTo().window(winHandle);
        }

        return menuLinkHref;

    }

    /*---------------Basket block---------------------------------------------------------------*/
    public Boolean menuItemIsActive (String id) {

        Boolean isActive = driver.findElement(mainMenu).findElement(By.id(id)).findElements(By.cssSelector(".m-main-i.active")).size()>0;
        return isActive;

    }

    public Boolean isBusketOpen() {
        return driver.findElements(By.id("cart-popup")).size()>0;
    }

    public void openCart() {

        if(driver.findElements(By.name("open_cart")).size()!=0) {
            driver.findElement(By.name("open_cart")).click();

            // switch focus of WebDriver to the next found window handle (that's your newly opened window)
            for (String winHandle : driver.getWindowHandles()) {
                driver.switchTo().window(winHandle);
            }

            (new WebDriverWait(driver, 20))
                    .until(ExpectedConditions.presenceOfElementLocated(By.id("cart-popup")));
        }
    }



}
